home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / t_os / gonza / key.c < prev    next >
C/C++ Source or Header  |  1993-07-08  |  10KB  |  469 lines

  1. /*
  2.             Key Master V0.1
  3.                                 presented by Gonza ( TOOLS ).
  4. */
  5.  
  6. #include    <egb.h>
  7. #include    <stdio.h>
  8. #include    <ctype.h>
  9. #include    <string.h>
  10. #include    <stdlib.h>
  11. #include    <time.h>
  12. #include    <fmc.h>
  13.  
  14. char gwork[EgbWorkSize], buf[300000];
  15. char filename[80],kindname[80],data[100][100],edata[100][100];
  16. int kind, dataend, max, err;
  17. time_t starttime, endtime;
  18. unsigned int alltime;
  19.  
  20. enum coltyp { black, ltcha, tuchi, dkgreen, blackgreen, haiblue,
  21.               ki, ltblue, akamura, red, birizi, uguisu, ltgreen,
  22.               whitecha, dkcha, white } color;
  23.  
  24. void palette_set( int color_num, int red, int green, int blue );
  25. void palette_init( void );
  26. void boxful(int x1, int y1, int x2, int y2, int c);
  27. void symbol(int x, int y, char *s, int c);
  28. void lineboxful(int x1, int y1, int x2, int y2, int c1, int c2);
  29. void windowbox(int x1, int y1, int x2, int y2 );
  30. void error( int i );
  31. void main( void );
  32. void mes( char *s, int c );
  33. void retkey( void );
  34. void put( int x1, int y1, int x2, int y2 );
  35. void get( int x1, int y1, int x2, int y2 );
  36. void opening( void );
  37. int mes2( char *s, int c );
  38. int set_x( char *s );
  39. void fileopen( void );
  40. void exec( void );
  41. void result( void );
  42. void xor_boxful( int x1, int y1, int x2, int y2, int c );
  43.  
  44. void main( void )
  45. {
  46.     int max;
  47.     
  48.     EGB_init( gwork, sizeof(gwork) );
  49.     KYB_init();
  50.     KYB_setcode( 0x0000 );
  51.     KYB_lock( 1 );
  52.     KYB_clic( 1 );
  53.     KYB_clrbuf();
  54.     palette_init();
  55.     KYB_lock( 0 );
  56.     opening();
  57.     fileopen();
  58.     exec();
  59.     result();
  60. }
  61.  
  62. /* オープニング */
  63. void opening( void )
  64. {
  65.     int k = 0;
  66.     boxful( 0, 0, 639, 479, dkgreen );
  67.     mes( "キーボードを準備してリターンキーを押してください", blackgreen );
  68.     mes( "キーボード打ち込み速度の測定 v0.1", blackgreen );
  69.     mes( "Programmed by GONZA ( tools )", blackgreen );
  70.     mes( "文章は  一般文(1), 一般文(2), 一般文(3)  の3つを用意しました", blackgreen );
  71.     mes( "デフォルトは英小モードです", akamura );
  72.     mes( "行末で改行する必要はありません", akamura );
  73.     
  74.     k = mes2( "選択してください ( 1: 一般文(1), 2:一般文(2), 3:一般文(3) )", akamura );
  75.     
  76.     switch( k )
  77.     {
  78.         case '1':
  79.             strcpy( filename, "english.dat" );
  80.             strcpy( kindname, "一般文(1)" );
  81.             kind = 1;
  82.             break;
  83.         case '2':
  84.             strcpy( filename, "english2.dat" );
  85.             strcpy( kindname, "一般文(2)" );
  86.             kind = 2;
  87.             break;
  88.         case '3':
  89.             strcpy( filename, "english3.dat" );
  90.             strcpy( kindname, "一般文(3)" );
  91.             kind = 3;
  92.             break;
  93.     }
  94.     mes( "リターンキーを押すと測定開始です", akamura );
  95. }
  96.  
  97. /* データファイルのオープン */
  98. void fileopen( void )
  99. {
  100.     FILE *fp;
  101.     int i = 0;
  102.     int len;
  103.     if(( fp = fopen( filename, "r" ) ) == NULL )
  104.         error( 1 );
  105.     
  106.     while( fgets( data[i], 256, fp ) != NULL )
  107.     {
  108.         len = strlen( data[i] );
  109.         data[ i++ ][ len-1 ] = '\0';
  110.     }
  111.     dataend = i - 1;
  112.     fclose( fp );
  113. }
  114.  
  115. /* タイプ速度測定 */
  116. void exec( void )
  117. {
  118.     int cn,len,x,er = 0,all = 0, i, mj = 0;
  119.     unsigned int moji, code, dmy, ipt;
  120.     
  121.     char s[5],ss[80],sss[80];
  122.     
  123.     for( cn=0; cn<=dataend; cn++ )
  124.     {
  125.         len = strlen( data[cn] ) - 1;
  126.         for( x=0; x<=len; x++ )
  127.             all++;
  128.     }
  129.     
  130.     time( &starttime );
  131.     
  132.     for( cn=0; cn<=dataend; cn++ )
  133.     {
  134.         sprintf( ss, "%d", cn );
  135.         strcat( ss, "行 / " );
  136.         sprintf( sss, "%d", dataend );
  137.         strcat( sss, "行" );
  138.         get( 10, 100, 634, 384 );
  139.         windowbox( 10, 100, 630, 380 );
  140.         symbol( 34, 116, data[cn], black );
  141.         symbol( 300, 176, ss, tuchi );
  142.         symbol( 400, 176, sss, blackgreen );
  143.         
  144.         len = strlen( data[cn] ) - 1;
  145.         for( x=0; x<=len; x++ )
  146.         {
  147.             mj++;
  148.             xor_boxful( 34+x*8, 148, 34+8+x*8, 148+16, ki );
  149.             /* symbol( 34 + x*8, 148, "_", red ); */
  150.             
  151.             KYB_clrbuf();
  152.             moji = KYB_read( 0, &code );
  153.             BPB_freeon( 10, 48 );
  154.             dmy = moji<<9;
  155.             dmy = dmy>>9;
  156.             
  157.             ipt = dmy;
  158.             xor_boxful( 34+x*8, 148, 34+8+x*8, 148+16, ki );
  159.             /* symbol( 34 + x*8, 148, "_", WHITE ); */
  160.             
  161.             s[0] =  (char)ipt;
  162.             s[1] = '\0';
  163.             
  164.             if( (char)ipt == data[cn][x] )
  165.             {
  166.                 symbol( 34 + x*8, 148, s, ltblue );
  167.                 edata[cn][x] = (char)ipt;
  168.             }
  169.             else
  170.             {
  171.                 symbol( 34 + x*8, 148, s, red );
  172.                 edata[cn][x] = (char)ipt;
  173.                 er++;
  174.             }
  175.         }
  176.         edata[cn][x+1] = '\0';
  177.         put( 10, 100, 634, 384 );
  178.     }
  179.     max = all;
  180.     err = er;
  181.     time( &endtime );
  182.     alltime = (unsigned int)difftime( endtime, starttime );
  183. }
  184.  
  185. /* 結果報告 */
  186. void result( void )
  187. {
  188.     int i, sc;
  189.     double ad, mx, ti, er;
  190.     char s[80],dmy[40];
  191.     for( i=0; i<=39; i++ )
  192.         dmy[i] = '\0';
  193.     mes( "ただいまの結果を報告いたします", blackgreen );
  194.     strcpy( s, "コース名: " );
  195.     strcat( s, kindname );
  196.     strcat( s, ", Time: " );
  197.     sprintf( dmy, "%d", alltime );
  198.     strcat( s, dmy );
  199.     strcat( s, ", Error: " );
  200.     for( i=0; i<=39; i++ )
  201.         dmy[i] = '\0';
  202.     sprintf( dmy, "%d", err );
  203.     strcat( s, dmy );
  204.     strcat( s, " / " );
  205.     sprintf( dmy, "%d", max );
  206.     strcat( s, dmy );
  207.     strcat( s, "letters. " );
  208.     mes( s, dkcha );
  209.     mx = (double)max;
  210.     ti = (double)alltime;
  211.     er = (double)err;
  212.     ad = mx/ti;
  213.     sc = (int)( (mx-er)/mx * ad * 100.00 );
  214.     
  215.     sprintf( dmy, "%f", ad );
  216.     strcpy( s, "タイプ速度: 毎秒 " );
  217.     strcat( s, dmy );
  218.     strcat( s, "文字" );
  219.     mes( s, dkcha );
  220.     sprintf( dmy, "%d", sc );
  221.     strcpy( s, "総合評価: " );
  222.     strcat( s, dmy );
  223.     strcat( s, "ポイントでした" );
  224.     mes( s, akamura );
  225.     mes( "では、またの御利用をお待ちいたしております。", blackgreen );
  226. }
  227.  
  228. /* リターンキー待ち */
  229. void retkey( void )
  230. {
  231.     unsigned int moji, code, dmy = '\0';
  232.     KYB_clrbuf();
  233.         while( dmy != 0x0d )
  234.         {
  235.             moji = KYB_read( 0, &code );
  236.             dmy = moji<<9;
  237.             dmy = dmy>>9;
  238.         }
  239. }
  240.  
  241. /* メッセージ */
  242. void mes( char *s, int c )
  243. {
  244.     int len, x1, y1, x2, y2;
  245.     
  246.     len = strlen( s );
  247.     x1 = (int)( 640 - len*8 ) / 2 - 48;
  248.     y1 = 200;
  249.     x2 = 640 - x1;
  250.     y2 = 280;
  251.     
  252.     get( x1, y1, x2+4, y2+4 );
  253.     windowbox( x1, y1, x2, y2 );
  254.     symbol( x1 + 48, 232, s, c );
  255.     retkey();
  256.     put( x1, y1, x2+4, y2+4 );
  257. }
  258.  
  259. /* メッセージ2 */
  260. int mes2( char *s, int c )
  261. {
  262.     int len, x1, y1, x2, y2;
  263.     unsigned int moji, code, k =0;
  264.     char ss[5];
  265.     
  266.     len = strlen( s );
  267.     x1 = (int)( 640 - len*8 ) / 2 - 48;
  268.     y1 = 200;
  269.     x2 = 640 - x1;
  270.     y2 = 280;
  271.     
  272.     get( x1, y1, x2+4, y2+4 );
  273.     windowbox( x1, y1, x2, y2 );
  274.     symbol( x1 + 48, 232, s, c );
  275.     while( k!='1' && k!='2' && k!='3' )
  276.     {
  277.         moji = KYB_read( 0, &code );
  278.         KYB_clrbuf();
  279.         k = moji<<8;
  280.         k = k>>8;
  281.     }
  282.     
  283.     put( x1, y1, x2+4, y2+4 );
  284.     return( k );
  285. }
  286.  
  287.  
  288. /* エラー処理 */
  289. void error( int i )
  290. {
  291.     int x = 0;
  292.     char s[121];
  293.     
  294.     windowbox( 120, 200, 520, 280 );
  295.     
  296.     switch( i )
  297.     {
  298.         case 1:
  299.             strcpy( s, "データファイルがありません" );
  300.             break;
  301.     }
  302.  
  303.     x = set_x( s );
  304.     symbol( x, 232, s, red );
  305.     retkey();
  306.     exit( 1 );
  307. }
  308.  
  309. int set_x( char *s )
  310. {
  311.     int i,x;
  312.     i = strlen( s );
  313.     x = 120 + ( 400 - i * 8 ) / 2;
  314.     return( x );
  315. }
  316.  
  317. /* Xor処理ボックスフル */
  318. void xor_boxful( int x1, int y1, int x2, int y2, int c )
  319. {
  320.     char para[10];
  321.  
  322.     EGB_color(gwork, 0, c);
  323.     EGB_color(gwork, 2, c);
  324.     EGB_paintMode( gwork, 0x22 );
  325.     EGB_writeMode( gwork, 4 );
  326.     WORD(para + 0) = x1;
  327.     WORD(para + 2) = y1;
  328.     WORD(para + 4) = x2;
  329.     WORD(para + 6) = y2;
  330.     EGB_rectangle(gwork, para);
  331.     EGB_writeMode( gwork, 0 );
  332. }
  333.  
  334.  
  335. /* パステルパレット設定 */
  336. void    palette_init( void )
  337. {
  338.     palette_set( black, 0, 0, 0 );
  339.     palette_set( ltcha, 9, 8, 5 );
  340.     palette_set( tuchi, 7, 5, 2 );
  341.     palette_set( dkgreen, 7, 9, 7 );
  342.     palette_set( blackgreen, 0, 5, 2 );
  343.     palette_set( haiblue, 9, 10, 11 );
  344.     palette_set( ki, 15, 15, 7 );
  345.     palette_set( ltblue, 9, 9, 13 );
  346.     palette_set( akamura, 12, 3, 8 );
  347.     palette_set( red, 15, 3, 0 );
  348.     palette_set( birizi, 0, 10, 5 );
  349.     palette_set( uguisu, 7, 9, 3 );
  350.     palette_set( ltgreen, 9, 13, 10 );
  351.     palette_set( whitecha, 14, 14, 12 );
  352.     palette_set( dkcha, 5, 3, 0 );
  353.     palette_set( white, 15, 15, 15 );
  354. }
  355.  
  356. /* オリジナルパレット設定 */
  357. void palette_set( int color_num, int red, int green, int blue )
  358. {
  359.     char para[64];
  360.     DWORD( para + 0 ) = 1; 
  361.     DWORD( para + 4 ) = color_num;
  362.     BYTE( para + 8 ) = blue*17;
  363.     BYTE( para + 9 ) = red*17;
  364.     BYTE( para + 10 ) = green*17;
  365.     BYTE( para + 11 ) = 0;
  366.  
  367.     EGB_palette( gwork, 1, para );
  368. }
  369.  
  370. /* ボックスフル */
  371. void boxful(int x1, int y1, int x2, int y2, int c)
  372. {
  373.     char para[10];
  374.  
  375.     EGB_color(gwork, 0, c);
  376.     EGB_color(gwork, 2, c);
  377.     EGB_paintMode(gwork, 0x22);
  378.     WORD(para + 0) = x1;
  379.     WORD(para + 2) = y1;
  380.     WORD(para + 4) = x2;
  381.     WORD(para + 6) = y2;
  382.     EGB_rectangle(gwork, para);
  383. }
  384.  
  385. void get( int x1, int y1, int x2, int y2 )
  386. {
  387.     char para[64];
  388.     
  389.     DWORD( para + 0 ) = (unsigned int)buf;
  390.     WORD( para + 4 ) = 0x14;
  391.     WORD( para + 6 ) = x1;
  392.     WORD( para + 8 ) = y1;
  393.     WORD( para +10 ) = x2;
  394.     WORD( para +12 ) = y2;
  395.     
  396.     EGB_getBlock( gwork, para );
  397. }
  398.  
  399. void put( int x1, int y1, int x2, int y2 )
  400. {
  401.     char para[64];
  402.     
  403.     DWORD( para + 0 ) = (unsigned int)buf;
  404.     WORD( para + 4 ) = 0x14;
  405.     WORD( para + 6 ) = x1;
  406.     WORD( para + 8 ) = y1;
  407.     WORD( para +10 ) = x2;
  408.     WORD( para +12 ) = y2;
  409.     
  410.     EGB_putBlock( gwork, 0, para );
  411. }
  412.  
  413.  
  414.  
  415. /* 枠付ボックスフル */
  416. void lineboxful(int x1, int y1, int x2, int y2, int c1, int c2)
  417. {
  418.     char para[10];
  419.  
  420.     EGB_color(gwork, 0, c1);
  421.     EGB_color(gwork, 2, c2);
  422.     EGB_paintMode(gwork, 0x22);
  423.     WORD(para + 0) = x1;
  424.     WORD(para + 2) = y1;
  425.     WORD(para + 4) = x2;
  426.     WORD(para + 6) = y2;
  427.     EGB_rectangle(gwork, para);
  428. }
  429.  
  430. /* tmenu v1.1式 ウインドウ */
  431. void windowbox( int x1, int y1, int x2, int y2 )
  432. {
  433.     char para[10];
  434.     
  435.     EGB_color( gwork, 0, dkcha );
  436.     EGB_color( gwork, 2, dkcha );
  437.     EGB_paintMode( gwork, 0x22 );
  438.     WORD(para + 0) = x1 + 4 ;
  439.     WORD(para + 2) = y1 + 4 ;
  440.     WORD(para + 4) = x2 + 4 ;
  441.     WORD(para + 6) = y2 + 4 ;
  442.     EGB_rectangle( gwork, para );
  443.  
  444.     EGB_color( gwork, 2, whitecha );
  445.     WORD(para + 0) = x1;
  446.     WORD(para + 2) = y1;
  447.     WORD(para + 4) = x2;
  448.     WORD(para + 6) = y2;
  449.     EGB_rectangle( gwork, para );
  450. }
  451.  
  452. /* ノーマルシンボル */
  453. void symbol(int x, int y, char *s, int c)
  454. {
  455.     char para[256];
  456.     int     len;
  457.     int     i;
  458.  
  459.     EGB_color(gwork, 0, c);
  460.     len = strlen(s);
  461.     WORD(para + 0) = x;
  462.     WORD(para + 2) = y + 16;
  463.     WORD(para + 4) = len;
  464.     for (i = 6; i < len + 6; i++)
  465.         para[i] = s[i - 6];
  466.     para[i] = '\0';
  467.     EGB_sjisString(gwork, para);
  468. }
  469.